package com.duan.rabbitmq.simple;

import com.duan.rabbitmq.utils.RabbitMqUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author db
 * @version 1.0
 * @description Consumer  消费者
 * @since 2022/12/29
 */
public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
//        ConnectionFactory connectionFactory = new ConnectionFactory();
//        connectionFactory.setHost("192.168.137.120");
//        connectionFactory.setPort(5672);
//        connectionFactory.setVirtualHost("/");
//        connectionFactory.setUsername("admin");
//        connectionFactory.setPassword("123456");
//        connectionFactory.setHandshakeTimeout(60000);
//
//        // 创建连接
//        Connection connection = connectionFactory.newConnection();

        // 从工具类中获得连接
        Connection connection = RabbitMqUtil.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();

        // 消费者成功消费时的回调
        DeliverCallback deliverCallback = (consumerTag,message) ->{
            System.out.println(new String(message.getBody()));
        };

        // 消费者取消消费时的回调
        CancelCallback callback = consumerTag ->{
            System.out.println("消费者取消消费接口的回调");
        };

        // 参数1：消费队列的名称
        // 参数2：消息的自动确认机制（已获得消息就通知MQ消息已被消费）true 打开 false 关闭
        // 参数3：
        channel.basicConsume("hello",true,deliverCallback,callback);

//        channel.close();
//        connection.close();
    }
}














